package ee.ut.math.chess.hadoop.dummy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.game.model.Game;
import ee.ut.math.chess.hadoop.HadoopManager;

public class DummyHadoopManager extends HadoopManager {
    public static void doComputerMove(final Game game) {
        threadPool.execute(new Runnable() {

            public void run() {
                try {
                    System.out.println("Hadoop thinking");

                    DummySkeleton skeleton = new DummySkeleton();
                    Move move = skeleton.getMove(game);

                    System.out.println("Hadoop moving");
                    game.move(move);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public static byte[] getBytesFromGame(Game game) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(out);
        os.writeObject(game);
        return out.toByteArray();
    }

    public static Game getGameFromBytes(byte[] data) throws IOException, ClassNotFoundException {
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ObjectInputStream is = new ObjectInputStream(in);
        return (Game) is.readObject();
    }
    
}
